const express = require('express');
//引入模型对象
const Users = require('../models/users');
const router = new express.Router();

//定义用户名密码规则
const user_name_reg = /^[A-Za-z0-9]{5,16}$/
const password_reg = /^[A-Za-z0-9]{0,20}$/

//登录
router.post('/login',async(req,res)=>{
    const {user_name,password} = req.body;
    if(user_name_reg.test(user_name) && password_reg.test(password)){
        try{
            const user = await Users.findOne({user_name});
            if(user){
                if(user.password == password){
                    res.send('登录成功！')
                }else{
                    res.send('密码错误！')
                }
            }else{
                res.send('该用户不存在，请注册！');
            }
        }catch(e){
            console.log(e);
            res.send('网络延迟！')
        }
    }
})
module.exports = router;